﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using Microsoft.Practices.Mobile.ContainerModel;
using EditUsers_Best;
using System.IO;
using System.Reflection;
using System.Drawing;

namespace EditUsers_Best.ManageUsersModule
{
    public class ManageUsersModuleConfigurator : IContainerModule
	{
		ITaskService tasks;

        public ManageUsersModuleConfigurator(ITaskService tasks)
		{
			this.tasks = tasks;
		}

		public void Configure(Container container)
		{
			// Register the presenter and view types.
            RegisterTypes(container);
			// Register the launch tasks for the main screen.
			RegisterTasks(container);
		}

		private void RegisterTasks(Container container)
		{
			// This is the entry point to our module.
            tasks.Register<IManageUsersView>("Edit Customers", Properties.Resources.ManageUsersIcon);
        }

        private void RegisterTypes(Container container)
		{
            // Register the IUserRepository type, with its factory function and also
            // (just for fun) some test data entered through an InitializedBy anon function.
            container.Register<IUserRepository>(
					c => new MemoryUserRepository())
				.InitializedBy((c, r) =>
				{
					// Initialize with some fake data.
					r.Create(new User { UserName = "wigley_1", FirstName = "Andy", LastName = "Wigley" });
					r.Create(new User { UserName = "blackb_1", FirstName = "Andrew", LastName = "Blackburn" });
				});

            // Register the View. The InitializedBy function here asks the container to
            // resolve the ManageUsersPresenter (i.e. create one if we haven't got one)
            // and set the views' Presenter property to that instance.
            container
				.Register<IManageUsersView>(
					c => new ManageUsersView())
				.InitializedBy(
					(c, v) => v.Presenter = c.Resolve<ManageUsersPresenter>())
				.ReusedWithin(ReuseScope.Container);

            // Register the factory function for the Presenter.
            // We use an initializedBy function here to do an initial serach so that
            // the view gets filled with the current data.
            container.Register(c => new ManageUsersPresenter(
					c.Resolve<IManageUsersView>(),
					c.Resolve<IUserRepository>()))
                .InitializedBy(
                    (c, p) => p.Search(string.Empty))
				.ReusedWithin(ReuseScope.Container);
		}
	}
}
